package zengjin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.common.BeanCopyUtil;
import zengjin.common.PageResponse;
import zengjin.mapper.*;
import zengjin.model.dto.verification.AddVerificationRequest;
import zengjin.model.dto.verification.PageVerificationRequest;
import zengjin.model.dto.verification.UpdateVerificationRequest;
import zengjin.model.en.*;
import zengjin.model.vo.verification.PageVerificationVO;
import zengjin.model.vo.verification.VerificationAndEmployerDetailVO;
import zengjin.model.vo.verification.VerificationAndResumeDetailVO;
import zengjin.model.vo.verification.verificationAndJobseekerDetailVO;
import zengjin.service.VerificationService;

import java.util.Date;
import java.util.List;

/**
 * @author 19324
 * @description 针对表【verification】的数据库操作Service实现
 * @createDate 2025-03-10 15:28:39
 */
@Service
public class VerificationServiceImpl extends ServiceImpl<VerificationMapper, Verification>
        implements VerificationService {

    @Resource
    private VerificationMapper verificationMapper;

    @Resource
    private EmployerMapper employerMapper;

    @Resource
    private ImageMapper imageMapper;

    @Resource
    private JobseekerMapper jobseekerMapper;

    @Resource
    private ResumeMapper resumeMapper;

    @Override
    public Boolean addVerification(AddVerificationRequest addVerificationRequest) {
        //1.获取参数
        String verificationType = addVerificationRequest.getVerificationType();
        Long verificationCreaterId = addVerificationRequest.getVerificationCreaterId();
        String verificationCreater = addVerificationRequest.getVerificationCreater();
        String verificationStatus = addVerificationRequest.getVerificationStatus();
        List<String> verificationFileList = addVerificationRequest.getVerificationFileList();
        String verificationRemarks = addVerificationRequest.getVerificationRemarks();

        String result = String.join(",", verificationFileList);

        //2.封装对象
        Verification verification = new Verification();
        verification.setVerificationType(verificationType);
        verification.setVerificationCreaterId(verificationCreaterId);
        verification.setVerificationCreater(verificationCreater);
        verification.setVerificationStatus(verificationStatus);
        verification.setVerificationFileList(result);
        verification.setVerificationRemarks(verificationRemarks);

        //3.保存
        int insert = verificationMapper.insert(verification);
        return insert > 0;
    }

    @Override
    public PageResponse<PageVerificationVO> getVerificationPage(PageVerificationRequest pageVerificationRequest) {
        //1.获取参数
        String verificationType = pageVerificationRequest.getVerificationType();
        String verificationStatus = pageVerificationRequest.getVerificationStatus();
        String searchName = pageVerificationRequest.getSearchName();

        //2.构建分页条件
        Page<Verification> page = pageVerificationRequest.toPageDefaultSortByUpdateTime();

        //3.分页查询
        Page<Verification> verificationPage = lambdaQuery()
                .eq(StrUtil.isNotBlank(verificationType), Verification::getVerificationType, verificationType)
                .eq(StrUtil.isNotBlank(verificationStatus), Verification::getVerificationStatus, verificationStatus)
                .like(StrUtil.isNotBlank(searchName), Verification::getVerificationCreater, searchName)
                .page(page);

        return PageResponse.of(verificationPage, PageVerificationVO.class);

    }

    @Override
    public Object getVerificationDetail(Long verificationId) {
        //1.查询出认证信息
        Verification verification = verificationMapper.selectById(verificationId);
        //2.判断是是什么认证
        if ("企业认证".equals(verification.getVerificationType())) {
            //3.如果是雇主认证，则查询雇主信息
            VerificationAndEmployerDetailVO verificationAndEmployerDetailVO;
            Employer employer = employerMapper.selectById(verification.getVerificationCreaterId());
            verificationAndEmployerDetailVO = BeanCopyUtil.copyPropertiesToVo(employer, verification, VerificationAndEmployerDetailVO.class);
            //查询出企业头像
            QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("image_name", "employer_avatar");
            queryWrapper.eq("image_name_id", employer.getEmployerId());
            Image image = imageMapper.selectOne(queryWrapper);
            verificationAndEmployerDetailVO.setUserAvatarUrl(image.getImageUrl());

            return verificationAndEmployerDetailVO;
        } else if ("学生认证".equals(verification.getVerificationType())) {
            //4.如果是学生认证，则查询学生信息
            verificationAndJobseekerDetailVO verificationAndJobseekerDetailVO;
            Jobseeker jobseeker = jobseekerMapper.selectById(verification.getVerificationCreaterId());
            verificationAndJobseekerDetailVO = BeanCopyUtil.copyPropertiesToVo(jobseeker, verification, verificationAndJobseekerDetailVO.class);
            //查询出求职者头像
            QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("image_name", "jobseeker_avatar");
            queryWrapper.eq("image_name_id", jobseeker.getJobseekerId());
            Image image = imageMapper.selectOne(queryWrapper);
            verificationAndJobseekerDetailVO.setUserAvatarUrl(image.getImageUrl());

            return verificationAndJobseekerDetailVO;


        } else if ("荣誉认证".equals(verification.getVerificationType())) {
            //5.如果是个人认证，则查询个人认证信息
            VerificationAndResumeDetailVO verificationAndResumeDetailVO;
            Jobseeker jobseeker = jobseekerMapper.selectById(verification.getVerificationCreaterId());
            //查询出荣誉证书
            QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("resume_jobseeker_id", jobseeker.getJobseekerId());
            Resume resume = resumeMapper.selectOne(queryWrapper);
            String resumeCertificates = resume.getResumeCertificates();
            verificationAndResumeDetailVO = BeanCopyUtil.copyPropertiesToVo(jobseeker, verification, VerificationAndResumeDetailVO.class);
            verificationAndResumeDetailVO.setResumeId(resume.getResumeId());
            verificationAndResumeDetailVO.setResumeCertificatesVerified(resume.getResumeCertificatesVerified());
            verificationAndResumeDetailVO.setResumeCertificates(resumeCertificates);
            return verificationAndResumeDetailVO;
        }
        return null;
    }

    @Override
    public Boolean updateVerification(UpdateVerificationRequest updateVerificationRequest) {
        //1.获取参数
        Long verificationId = updateVerificationRequest.getVerificationId();
        String verificationStatus = updateVerificationRequest.getVerificationStatus();
        Date verificationPassDate = updateVerificationRequest.getVerificationPassDate();
        String verificationRefuse = updateVerificationRequest.getVerificationRefuse();

        //2.调用mapper
        UpdateWrapper<Verification> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("verification_id", verificationId);
        updateWrapper.set("verification_status", verificationStatus);
        updateWrapper.set("verification_pass_date", verificationPassDate);
        updateWrapper.set("verification_refuse", verificationRefuse);

        int update = verificationMapper.update(null, updateWrapper);
        return update > 0;

    }
}




